Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Immutable object</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Immutable_object"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Immutable_object rootpage-Immutable_object skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Immutable object</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">"Immutable" and "Immutability" redirect here. For the Christian doctrine, see <a href="Immutability_(theology)" title="Immutability (theology)">Immutability (theology)</a>. For the album, see <a href="Immutable_(album)" title="Immutable (album)">Immutable (album)</a>. For the crypto gaming company, see <a href="Immutable_Pty_Ltd" class="mw-redirect" title="Immutable Pty Ltd">Immutable Pty Ltd</a>.</div>
<p>In <a href="Object-oriented_computer_programming" class="mw-redirect" title="Object-oriented computer programming">object-oriented</a> (OO) and <a href="Functional_programming" title="Functional programming">functional</a> programming, an <b>immutable object</b> (unchangeable<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> object) is an <a href="Object_(computer_science)" title="Object (computer science)">object</a> whose <a href="State_(computer_science)" title="State (computer science)">state</a> cannot be modified after it is created.<sup id="cite_ref-Goetz_2-0" class="reference"><a href="#cite_note-Goetz-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> This is in contrast to a <b>mutable object</b> (changeable object), which can be modified after it is created.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses <a href="Memoization" title="Memoization">memoization</a> to cache the results of expensive computations could still be considered an immutable object.
</p><p>Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming. Immutable objects are also useful because they are inherently <a href="Thread_safety" title="Thread safety">thread-safe</a>.<sup id="cite_ref-Goetz_2-1" class="reference"><a href="#cite_note-Goetz-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects.<sup id="cite_ref-Goetz_2-2" class="reference"><a href="#cite_note-Goetz-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Concepts">Concepts</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Immutable_variables">Immutable variables</h3></div>
<p>In <a href="Imperative_programming" title="Imperative programming">imperative programming</a>, values held in <a href="Program_variable" class="mw-redirect" title="Program variable">program variables</a> whose content never changes are known as <i><a href="Constant_(programming)" class="mw-redirect" title="Constant (programming)">constants</a></i> to differentiate them from variables that could be altered during execution. Examples include conversion factors from meters to feet, or the value of <a href="Pi" title="Pi">pi</a> to several decimal places.
</p><p>Read-only fields may be calculated when the program runs (unlike constants, which are known beforehand), but never change after they are initialized.
</p>
<div class="mw-heading mw-heading3"><h3 id="Weak_vs_strong_immutability">Weak vs strong immutability</h3></div>
<p>Sometimes, one talks of certain <i>fields</i> of an object being immutable. This means that there is no way to change those parts of the object state, even though other parts of the object may be changeable (<i>weakly immutable</i>). If all fields are immutable, then the object is immutable. If the whole object cannot be extended by another class, the object is called <i>strongly immutable</i>.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> This might, for example, help to explicitly enforce certain invariants about certain data in the object staying the same through the lifetime of the object. In some languages, this is done with a keyword (e.g. <code>const</code> in <a href="C%2B%2B" title="C++">C++</a>, <code>final</code> in <a href="Java_(programming_language)" title="Java (programming language)">Java</a>) that designates the field as immutable. Some languages reverse it: in <a href="OCaml" title="OCaml">OCaml</a>, fields of an object or record are by default immutable, and must be explicitly marked with <code>mutable</code> to be so.
</p>
<div class="mw-heading mw-heading3"><h3 id="References_to_objects">References to objects</h3></div>
<p>In most <a href="Object-oriented_programming_language" class="mw-redirect" title="Object-oriented programming language">object-oriented languages</a>, objects can be referred to using <a href="Reference_(computer_science)" title="Reference (computer science)">references</a>. Some examples of such languages are <a href="Java_(programming_language)" title="Java (programming language)">Java</a>, <a href="C%2B%2B" title="C++">C++</a>, <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>, <a href="VB.NET" class="mw-redirect" title="VB.NET">VB.NET</a>, and many <a href="Scripting_language" title="Scripting language">scripting languages</a>, such as <a href="Perl" title="Perl">Perl</a>, <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, and <a href="Ruby_programming_language" class="mw-redirect" title="Ruby programming language">Ruby</a>. In this case, it matters whether the state of an object can vary when objects are shared via references.
</p>
<div class="mw-heading mw-heading3"><h3 id="Referencing_vs_copying_objects">Referencing vs copying objects</h3></div>
<p>If an object is known to be immutable, it is preferred to create a <a href="Reference_(computer_science)" title="Reference (computer science)">reference</a> of it instead of copying the entire object. This is done to conserve memory by preventing data duplication and avoid calls to constructors and destructors; it also results in a potential boost in execution speed.
</p><p>The reference copying technique is much more difficult to use for mutable objects, because if any user of a mutable object reference changes it, all other users of that reference see the change. If this is not the intended effect, it can be difficult to notify the other users to have them respond correctly. In these situations, <a href="Defensive_copy" class="mw-redirect" title="Defensive copy">defensive copying</a> of the entire object rather than the reference is usually an easy but costly solution. The <a href="Observer_pattern" title="Observer pattern">observer pattern</a> is an alternative technique for handling changes to mutable objects.
</p>
<div class="mw-heading mw-heading3"><h3 id="Copy-on-write">Copy-on-write</h3></div>
<p>A technique that blends the advantages of <b>mutable</b> and <b>immutable</b> objects, and is supported directly in almost all modern hardware, is <a href="Copy-on-write" title="Copy-on-write">copy-on-write</a> (COW). Using this technique, when a user asks the system to copy an object, it instead merely creates a new reference that still points to the same object. As soon as a user attempts to modify the object through a particular reference, the system makes a real copy, applies the modification to that, and sets the reference to refer to the new copy. The other users are unaffected, because they still refer to the original object. Therefore, under COW, all users appear to have a mutable version of their objects, although in the case that users do not modify their objects, the space-saving and speed advantages of immutable objects are preserved. Copy-on-write is popular in <a href="Virtual_memory" title="Virtual memory">virtual memory</a> systems because it allows them to save memory space while still correctly handling anything an application program might do.
</p>
<div class="mw-heading mw-heading3"><h3 id="Interning">Interning</h3></div>
<p>The practice of always using references in place of copies of equal objects is known as <i><a href="Intern_(computer_science)" class="mw-redirect" title="Intern (computer science)">interning</a></i>. If interning is used, two objects are considered equal <a href="If_and_only_if" title="If and only if">if and only if</a> their references, typically represented as pointers or integers, are equal. Some languages do this automatically: for example, <a href="Python_(programming_language)" title="Python (programming language)">Python</a> automatically <a href="String_intern_pool" class="mw-redirect" title="String intern pool">interns short strings</a>. If the algorithm that implements interning is guaranteed to do so in every case that it is possible, then comparing objects for equality is reduced to comparing their pointers – a substantial gain in speed in most applications. (Even if the algorithm is not guaranteed to be comprehensive, there still exists the possibility of a <a href="Fast_path" title="Fast path">fast path</a> case improvement when the objects are equal and use the same reference.) Interning is generally only useful for immutable objects.
</p>
<div class="mw-heading mw-heading3"><h3 id="Thread_safety">Thread safety</h3></div>
<p>Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered more <i><a href="Thread-safe" class="mw-redirect" title="Thread-safe">thread-safe</a></i> than mutable objects.
</p>
<div class="mw-heading mw-heading3"><h3 id="Violating_immutability">Violating immutability</h3></div>
<p>Immutability does not imply that the object as stored in the computer's <a href="Computer_storage" class="mw-redirect" title="Computer storage">memory</a> is unwriteable. Rather, immutability is a <a href="Compile-time" class="mw-redirect" title="Compile-time">compile-time</a> construct that indicates what a programmer can do through the normal interface of the object, not necessarily what they can absolutely do (for instance, by circumventing the <a href="Type_system" title="Type system">type system</a> or violating <a href="Const_correctness" class="mw-redirect" title="Const correctness">const correctness</a> in <a href="C_(programming_language)" title="C (programming language)">C</a> or <a href="C%2B%2B" title="C++">C++</a>).
</p>
<div class="mw-heading mw-heading2"><h2 id="Language-specific_details">Language-specific details</h2></div>
<p>In <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, <a href="Java_(programming_language)" title="Java (programming language)">Java</a><sup id="cite_ref-Bloch_5-0" class="reference"><a href="#cite_note-Bloch-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup><sup class="reference nowrap"><span title="Page / location: 80">: 80 </span></sup> and the <a href=".NET_Framework" title=".NET Framework">.NET Framework</a>, strings are immutable objects. Both Java and the .NET Framework have mutable versions of string. In Java<sup id="cite_ref-Bloch_5-1" class="reference"><a href="#cite_note-Bloch-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup><sup class="reference nowrap"><span title="Page / location: 84">: 84 </span></sup> these are <code><a href="StringBuffer" class="mw-redirect" title="StringBuffer">StringBuffer</a></code> and <code><a href="StringBuilder" class="mw-redirect" title="StringBuilder">StringBuilder</a></code> (mutable versions of Java <code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/String.html">String</a></code>) and in .NET this is <code><a rel="nofollow" class="external text" href="http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.aspx">StringBuilder</a></code> (mutable version of .Net <code><a rel="nofollow" class="external text" href="http://msdn2.microsoft.com/en-us/library/system.string.aspx">String</a></code>). <a href="Python_3" class="mw-redirect" title="Python 3">Python 3</a> has a mutable string (bytes) variant, named <code>bytearray</code>.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p><p>Additionally, all of the <a href="Primitive_wrapper_class" class="mw-redirect" title="Primitive wrapper class">primitive wrapper classes</a> in Java are immutable.
</p><p>Similar patterns are the <a href="Immutable_interface" title="Immutable interface">Immutable Interface</a> and Immutable Wrapper.
</p><p>In pure <a href="Functional_programming" title="Functional programming">functional programming</a> languages it is not possible to create mutable objects without extending the language (e.g. via a mutable references library or a <a href="Foreign_function_interface" title="Foreign function interface">foreign function interface</a>), so all objects are immutable.
</p>
<div class="mw-heading mw-heading3"><h3 id="Ada">Ada</h3></div>
<p>In <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>, any object is declared either <i>variable</i> (i.e. mutable; typically the implicit default), or <code>constant</code> (i.e. immutable) via the <code>constant</code> keyword.
</p>
<div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre> <span class="kd">type</span> <span class="kt">Some_type</span> <span class="kr">is</span> <span class="kr">new</span> <span class="kt">Integer</span><span class="p">;</span> <span class="c1">-- could be anything more complicated</span>
<span class="no">x</span><span class="p">:</span> <span class="kr">constant</span> <span class="n">Some_type</span><span class="p">:=</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">-- immutable</span>
<span class="n">y</span><span class="p">:</span> <span class="n">Some_type</span><span class="p">;</span> <span class="c1">-- mutable</span>
</pre></div>
<p>Subprogram parameters are immutable in the <i>in</i> mode, and mutable in the <i>in out</i> and <i>out</i> modes.
</p>
<div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre> <span class="kd">procedure</span> <span class="nf">Do_it</span><span class="p">(</span><span class="nv">a</span><span class="p">: </span><span class="nv">in</span> <span class="nv">Integer</span><span class="p">;</span> <span class="nv">b</span><span class="p">: </span><span class="nv">in</span> <span class="nv">out</span> <span class="nv">Integer</span><span class="p">;</span> <span class="nv">c</span><span class="p">: </span><span class="nv">out</span> <span class="nv">Integer</span><span class="p">)</span> <span class="kr">is</span>
<span class="kr">begin</span>
<span class="c1">-- a is immutable</span>
<span class="n">b</span><span class="p">:=</span> <span class="n">b</span> <span class="o">+</span> <span class="n">a</span><span class="p">;</span>
<span class="n">c</span><span class="p">:=</span> <span class="n">a</span><span class="p">;</span>
<span class="kr">end</span> <span class="nf">Do_it</span><span class="p">;</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="C#">C#</h3></div>
<p>In <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> you can enforce immutability of the fields of a class with the <code>readonly</code> statement.<sup id="cite_ref-Skeet_7-0" class="reference"><a href="#cite_note-Skeet-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup><sup class="reference nowrap"><span title="Page / location: 239">: 239 </span></sup>
By enforcing all the fields as immutable, you obtain an immutable type.
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">AnImmutableType</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">readonly</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">_value</span><span class="p">;</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="nf">AnImmutableType</span><span class="p">(</span><span class="kt">double</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span>
<span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="n">_value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">AnImmutableType</span><span class="w"> </span><span class="nf">Square</span><span class="p">()</span><span class="w"> </span>
<span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="nf">AnImmutableType</span><span class="p">(</span><span class="n">_value</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">_value</span><span class="p">);</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>C# have records which are immutable.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">record</span><span class="w"> </span><span class="nf">Person</span><span class="p">(</span><span class="kt">string</span><span class="w"> </span><span class="n">FirstName</span><span class="p">,</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="n">LastName</span><span class="p">);</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="C++">C++</h3></div>
<p>In C++, a <a href="Const-correctness" class="mw-redirect" title="Const-correctness">const-correct</a> implementation of <code>Cart</code> would allow the user to create instances of the class and then use them as either <code>const</code> (immutable) or mutable, as desired, by providing two different versions of the <code>items()</code> method. (Notice that in C++ it is not necessary — and in fact impossible — to provide a specialized constructor for <code>const</code> instances.)
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">Cart</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="n">Cart</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;</span><span class="w"> </span><span class="n">items</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="n">items_</span><span class="p">(</span><span class="n">items</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span>

<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">items</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">items_</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">items</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">items_</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ComputeTotalCost</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* return sum of the prices */</span><span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;</span><span class="w"> </span><span class="n">items_</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<p>Note that, when there is a data member that is a pointer or reference to another object, then it is possible to mutate the object pointed to or referenced only within a non-const method.
</p><p>C++ also provides abstract (as opposed to bitwise) immutability via the <code>mutable</code> keyword, which lets a <a href="Member_variable" title="Member variable">member variable</a> be changed from within a <code>const</code> method.
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">Cart</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="n">Cart</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;</span><span class="w"> </span><span class="n">items</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="n">items_</span><span class="p">(</span><span class="n">items</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span>

<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">items</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">items_</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ComputeTotalCost</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">total_cost_</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">*</span><span class="n">total_cost_</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total_cost</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="k">auto</span><span class="o">&amp;</span><span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">items_</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">total_cost</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">Cost</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">total_cost_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total_cost</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">total_cost</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;</span><span class="w"> </span><span class="n">items_</span><span class="p">;</span>
<span class="w"> </span><span class="k">mutable</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">total_cost_</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="D">D</h3></div>
<p>In <a href="D_(programming_language)" title="D (programming language)">D</a>, there exist two <a href="Type_qualifier" title="Type qualifier">type qualifiers</a>, <code>const</code> and <code>immutable</code>, for variables that cannot be changed.<sup id="cite_ref-d_spec_const_10-0" class="reference"><a href="#cite_note-d_spec_const-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> Unlike C++'s <code>const</code>, Java's <code>final</code>, and C#'s <code>readonly</code>, they are transitive and recursively apply to anything reachable through references of such a variable. The difference between <code>const</code> and <code>immutable</code> is what they apply to: <code>const</code> is a property of the variable: there might legally exist mutable references to referred value, i.e. the value can actually change. In contrast, <code>immutable</code> is a property of the referred value: the value and anything transitively reachable from it cannot change (without breaking the type system, leading to <a href="Undefined_behavior" title="Undefined behavior">undefined behavior</a>). Any reference of that value must be marked <code>const</code> or <code>immutable</code>. Basically for any unqualified type <code>T</code>, <code>const(T)</code> is the disjoint union of <code>T</code> (mutable) and <code>immutable(T)</code>.
</p>
<div class="mw-highlight mw-highlight-lang-d mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="cm">/*mutable*/</span><span class="w"> </span><span class="n">Object</span><span class="w"> </span><span class="n">mField</span><span class="p">;</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">Object</span><span class="w"> </span><span class="n">cField</span><span class="p">;</span>
<span class="w"> </span><span class="k">immutable</span><span class="w"> </span><span class="n">Object</span><span class="w"> </span><span class="n">iField</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>For a mutable <code>C</code> object, its <code>mField</code> can be written to. For a <code>const(C)</code> object, <code>mField</code> cannot be modified, it inherits <code>const</code>; <code>iField</code> is still immutable as it is the stronger guarantee. For an <code>immutable(C)</code>, all fields are immutable.
</p><p>In a function like this:
</p>
<div class="mw-highlight mw-highlight-lang-d mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="n">func</span><span class="p">(</span><span class="n">C</span><span class="w"> </span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="k">immutable</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="n">i</span><span class="p">)</span>
<span class="p">{</span><span class="w"> </span><span class="cm">/* inside the braces */</span><span class="w"> </span><span class="p">}</span>
</pre></div>
<p>Inside the braces, <code>c</code> might refer to the same object as <code>m</code>, so mutations to <code>m</code> could indirectly change <code>c</code> as well. Also,
<code>c</code> might refer to the same object as <code>i</code>, but since the value then is immutable, there are no changes. However, <code>m</code> and <code>i</code> cannot legally refer to the same object.
</p><p>In the language of guarantees, mutable has no guarantees (the function might change the object), <code>const</code> is an outward-only guarantee that the function will not change anything, and
<code>immutable</code> is a bidirectional guarantee (the function will not change the value and the caller must not change it).
</p><p>Values that are <code>const</code> or <code>immutable</code> must be initialized by direct assignment at the point of <a href="Declaration_(computer_programming)" title="Declaration (computer programming)">declaration</a> or by a <a href="Constructor_(object-oriented_programming)" title="Constructor (object-oriented programming)">constructor</a>.
</p><p>Because <code>const</code> parameters forget if the value was mutable or not, a similar construct, <code>inout</code>, acts, in a sense, as a variable for mutability information.
A function of type <code>const(S) function(const(T))</code> returns <code>const(S)</code> typed values for mutable, const and immutable arguments. In contrast, a function of type <code>inout(S) function(inout(T))</code> returns <code>S</code> for mutable <code>T</code> arguments, <code>const(S)</code> for <code>const(T)</code> values, and <code>immutable(S)</code> for <code>immutable(T)</code> values.
</p><p>Casting immutable values to mutable inflicts undefined behavior upon change, even if the original value comes from a mutable origin. Casting mutable values to immutable can be legal when there remain no mutable references afterward. "An expression may be converted from mutable (...) to immutable if the expression is unique and all expressions it transitively refers to are either unique or immutable."<sup id="cite_ref-d_spec_const_10-1" class="reference"><a href="#cite_note-d_spec_const-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> If the <a href="Compiler" title="Compiler">compiler</a> cannot prove uniqueness, the casting can be done explicitly and it is up to the programmer to ensure that no mutable references exist.
</p><p>The type <code>string</code> is an alias for <code>immutable(char)[]</code>, i.e. a typed slice of memory of immutable characters.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> Making substrings is cheap, as it just copies and modifies a pointer and a length filed, and safe, as the underlying data cannot be changed. Objects of type <code>const(char)[]</code> can refer to strings, but also to mutable buffers.
</p><p>Making a shallow copy of a const or immutable value removes the outer layer of immutability: Copying an immutable string (<code>immutable(char[])</code>) returns a string (<code>immutable(char)[]</code>). The immutable pointer and length are being copied and the copies are mutable. The referred data has not been copied and keeps its qualifier, in the example <code>immutable</code>. It can be stripped by making a depper copy, e.g. using the <code>dup</code> function.
</p>
<div class="mw-heading mw-heading3"><h3 id="Java">Java</h3></div>
<p>A classic example of an immutable object is an instance of the Java <code>String</code> class
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="n">String</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"ABC"</span><span class="p">;</span>
<span class="n">s</span><span class="p">.</span><span class="na">toLowerCase</span><span class="p">();</span><span class="w"> </span><span class="c1">// This accomplishes nothing!</span>
</pre></div>
<p>The method <code>toLowerCase()</code> does not change the data "ABC" that <code>s</code> contains. Instead, a new String object is instantiated and given the data "abc" during its construction. A reference to this String object is returned by the <code>toLowerCase()</code> method. To make the String <code>s</code> contain the data "abc", a different approach is needed:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="na">toLowerCase</span><span class="p">();</span>
</pre></div>
<p>Now the String <code>s</code> references a new String object that contains "abc". There is nothing in the syntax of the <i>declaration</i> of the class String that enforces it as immutable; rather, none of the String class's methods ever affect the data that a String object contains, thus making it immutable.
</p><p>The keyword <code>final</code> (<a href="Final_(Java)#Final_variables" title="Final (Java)">detailed article</a>) is used in implementing immutable primitive types and object references,<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> but it cannot, by itself, make <i>the objects themselves</i> immutable. See below examples:
</p><p>Primitive type variables (<code>int</code>, <code>long</code>, <code>short</code>, etc.) can be reassigned after being defined. This can be prevented by using <code>final</code>.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"> </span><span class="c1">//int is a primitive type</span>
<span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">43</span><span class="p">;</span><span class="w"> </span><span class="c1">// OK</span>

<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span>
<span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">43</span><span class="p">;</span><span class="w"> </span><span class="c1">// does not compile. j is final so can't be reassigned</span>
</pre></div>
<p>Reference types cannot be made immutable just by using the <code>final</code> keyword. <code>final</code> only prevents reassignment.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">MyObject</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">MyObject</span><span class="p">();</span><span class="w"> </span><span class="c1">//m is of reference type</span>
<span class="n">m</span><span class="p">.</span><span class="na">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span><span class="w"> </span><span class="c1">// OK. We can change state of object m (m is mutable and final doesn't change this fact)</span>
<span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">MyObject</span><span class="p">();</span><span class="w"> </span><span class="c1">// does not compile. m is final so can't be reassigned</span>
</pre></div>
<p>Primitive wrappers (<code>Integer</code>, <code>Long</code>, <code>Short</code>, <code>Double</code>, <code>Float</code>, <code>Character</code>, <code>Byte</code>, <code>Boolean</code>) are also all immutable. Immutable classes can be implemented by following a few simple guidelines.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="JavaScript">JavaScript</h3></div>
<p>In <a href="JavaScript" title="JavaScript">JavaScript</a>, all primitive types (Undefined, Null, Boolean, Number, BigInt, String, Symbol) are immutable, but custom objects are generally mutable.
</p>
<div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span class="kd">function</span><span class="w"> </span><span class="nx">doSomething</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* does changing x here change the original? */</span><span class="w"> </span><span class="p">};</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'a string'</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">an</span><span class="o">:</span><span class="w"> </span><span class="s1">'object'</span><span class="w"> </span><span class="p">};</span>
<span class="nx">doSomething</span><span class="p">(</span><span class="nx">str</span><span class="p">);</span><span class="w"> </span><span class="c1">// strings, numbers and bool types are immutable, function gets a copy</span>
<span class="nx">doSomething</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span><span class="w"> </span><span class="c1">// objects are passed in by reference and are mutable inside function</span>
<span class="nx">doAnotherThing</span><span class="p">(</span><span class="nx">str</span><span class="p">,</span><span class="w"> </span><span class="nx">obj</span><span class="p">);</span><span class="w"> </span><span class="c1">// `str` has not changed, but `obj` may have.</span>
</pre></div>
<p>To simulate immutability in an object, one may define properties as read-only (writable: false).
</p>
<div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span class="kd">var</span><span class="w"> </span><span class="nx">obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">value</span><span class="o">:</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">,</span><span class="w"> </span><span class="nx">writable</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">});</span>
<span class="nx">obj</span><span class="p">.</span><span class="nx">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'bar2'</span><span class="p">;</span><span class="w"> </span><span class="c1">// silently ignored</span>
</pre></div>
<p>However, the approach above still lets new properties be added. Alternatively, one may use <a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze">Object.freeze</a> to make existing objects immutable.
</p>
<div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span class="kd">var</span><span class="w"> </span><span class="nx">obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">foo</span><span class="o">:</span><span class="w"> </span><span class="s1">'bar'</span><span class="w"> </span><span class="p">};</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">freeze</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span>
<span class="nx">obj</span><span class="p">.</span><span class="nx">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'bars'</span><span class="p">;</span><span class="w"> </span><span class="c1">// cannot edit property, silently ignored</span>
<span class="nx">obj</span><span class="p">.</span><span class="nx">foo2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'bar2'</span><span class="p">;</span><span class="w"> </span><span class="c1">// cannot add property, silently ignored</span>
</pre></div>
<p>With the implementation of <a rel="nofollow" class="external text" href="https://tc39.es/ecma262/#sec-let-and-const-declarations">ECMA262</a>, JavaScript has the ability to create immutable references that cannot be reassigned. However, using a <code>const</code> declaration doesn't mean that value of the read-only reference is immutable, just that the name cannot be assigned to a new value.
</p>
<div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span class="kd">const</span><span class="w"> </span><span class="nx">ALWAYS_IMMUTABLE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>

<span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">ALWAYS_IMMUTABLE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span>
<span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Can't reassign an immutable reference."</span><span class="p">);</span>
<span class="p">}</span>

<span class="kd">const</span><span class="w"> </span><span class="nx">arr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">];</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mf">4</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">arr</span><span class="p">);</span><span class="w"> </span><span class="c1">// [1, 2, 3, 4]</span>
</pre></div>
<p>The use of immutable state has become a rising trend in JavaScript since the introduction of <a href="React_(JavaScript_library)" class="mw-redirect" title="React (JavaScript library)">React</a>, which favours Flux-like state management patterns such as <a href="Redux_(JavaScript_library)" title="Redux (JavaScript library)">Redux</a>.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Perl">Perl</h3></div>
<p>In <a href="Perl" title="Perl">Perl</a>, one can create an immutable class with the Moo library by simply declaring all the attributes read only:
</p>
<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="k">package</span><span class="w"> </span><span class="nn">Immutable</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">Moo</span><span class="p">;</span>

<span class="n">has</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="n">is</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="s">'ro'</span><span class="p">,</span><span class="w"> </span><span class="c1"># read only</span>
<span class="w"> </span><span class="n">default</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="s">'data'</span><span class="p">,</span><span class="w"> </span><span class="c1"># can be overridden by supplying the constructor with</span>
<span class="w"> </span><span class="c1"># a value: Immutable-&gt;new(value =&gt; 'something else');</span>
<span class="p">);</span>

<span class="mi">1</span><span class="p">;</span>
</pre></div>
<p>Creating an immutable class used to require two steps: first, creating accessors (either automatically or manually) that prevent modification of object attributes, and secondly, preventing direct modification of the instance data of instances of that class (this was usually stored in a hash reference, and could be locked with Hash::Util's lock_hash function):
</p>
<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="k">package</span><span class="w"> </span><span class="nn">Immutable</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">strict</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">warnings</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">base</span><span class="w"> </span><span class="sx">qw(Class::Accessor)</span><span class="p">;</span>
<span class="c1"># create read-only accessors</span>
<span class="nn">__PACKAGE__</span><span class="o">-&gt;</span><span class="n">mk_ro_accessors</span><span class="p">(</span><span class="sx">qw(value)</span><span class="p">);</span>
<span class="k">use</span><span class="w"> </span><span class="nn">Hash::Util</span><span class="w"> </span><span class="s">'lock_hash'</span><span class="p">;</span>

<span class="k">sub</span><span class="w"> </span><span class="nf">new</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$class</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nv">$class</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">ref</span><span class="p">(</span><span class="nv">$class</span><span class="p">);</span>
<span class="w"> </span><span class="nb">die</span><span class="w"> </span><span class="s">"Arguments to new must be key =&gt; value pairs\n"</span>
<span class="w"> </span><span class="k">unless</span><span class="w"> </span><span class="p">(</span><span class="nv">@_</span><span class="w"> </span><span class="nv">%</span><span class="w"> </span><span class="nv">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">%defaults</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="s">'data'</span><span class="p">,</span>
<span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nv">%defaults</span><span class="p">,</span>
<span class="w"> </span><span class="nv">@_</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="nb">bless</span><span class="w"> </span><span class="nv">$obj</span><span class="p">,</span><span class="w"> </span><span class="nv">$class</span><span class="p">;</span>
<span class="w"> </span><span class="c1"># prevent modification of the object data</span>
<span class="w"> </span><span class="n">lock_hash</span><span class="w"> </span><span class="nv">%$obj</span><span class="p">;</span>
<span class="p">}</span>
<span class="mi">1</span><span class="p">;</span>
</pre></div>
<p>Or, with a manually written accessor:
</p>
<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="k">package</span><span class="w"> </span><span class="nn">Immutable</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">strict</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">warnings</span><span class="p">;</span>
<span class="k">use</span><span class="w"> </span><span class="nn">Hash::Util</span><span class="w"> </span><span class="s">'lock_hash'</span><span class="p">;</span>

<span class="k">sub</span><span class="w"> </span><span class="nf">new</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$class</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nv">$class</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">ref</span><span class="p">(</span><span class="nv">$class</span><span class="p">);</span>
<span class="w"> </span><span class="nb">die</span><span class="w"> </span><span class="s">"Arguments to new must be key =&gt; value pairs\n"</span>
<span class="w"> </span><span class="k">unless</span><span class="w"> </span><span class="p">(</span><span class="nv">@_</span><span class="w"> </span><span class="nv">%</span><span class="w"> </span><span class="nv">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">%defaults</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="s">'data'</span><span class="p">,</span>
<span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nv">%defaults</span><span class="p">,</span>
<span class="w"> </span><span class="nv">@_</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="nb">bless</span><span class="w"> </span><span class="nv">$obj</span><span class="p">,</span><span class="w"> </span><span class="nv">$class</span><span class="p">;</span>
<span class="w"> </span><span class="c1"># prevent modification of the object data</span>
<span class="w"> </span><span class="n">lock_hash</span><span class="w"> </span><span class="nv">%$obj</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1"># read-only accessor</span>
<span class="k">sub</span><span class="w"> </span><span class="nf">value</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$self</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">my</span><span class="w"> </span><span class="nv">$new_value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1"># trying to set a new value</span>
<span class="w"> </span><span class="nb">die</span><span class="w"> </span><span class="s">"This object cannot be modified\n"</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nv">$self</span><span class="o">-&gt;</span><span class="p">{</span><span class="n">value</span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="mi">1</span><span class="p">;</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="PHP">PHP</h3></div>
<p>In <a href="PHP" title="PHP">PHP</a> have readonly properties since version 8.1 and readonly classes since version 8.2.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span class="nx">readonly</span> <span class="k">class</span> <span class="nc">BlogData</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$title</span><span class="p">;</span>

<span class="k">public</span> <span class="nx">Status</span> <span class="nv">$status</span><span class="p">;</span>

<span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">(</span><span class="nx">string</span> <span class="nv">$title</span><span class="p">,</span> <span class="nx">Status</span> <span class="nv">$status</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">title</span> <span class="o">=</span> <span class="nv">$title</span><span class="p">;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">status</span> <span class="o">=</span> <span class="nv">$status</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Python">Python</h3></div>
<p>In <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, some built-in types (numbers, Booleans, strings, tuples, frozensets) are immutable, but custom classes are generally mutable. To simulate immutability in a class, one could override attribute setting and deletion to raise exceptions:
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">ImmutablePoint</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""An immutable class with two attributes 'x' and 'y'."""</span>

<span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"x"</span><span class="p">,</span> <span class="s2">"y"</span><span class="p">]</span>

<span class="k">def</span><span class="w"> </span><span class="fm">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">"Can not modify immutable instance."</span><span class="p">)</span>

<span class="fm">__delattr__</span> <span class="o">=</span> <span class="fm">__setattr__</span>

<span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="c1"># We can no longer use self.value = value to store the instance data</span>
<span class="c1"># so we must explicitly call the superclass</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__setattr__</span><span class="p">(</span><span class="s2">"x"</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__setattr__</span><span class="p">(</span><span class="s2">"y"</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
<p>The standard library helpers <a rel="nofollow" class="external text" href="https://docs.python.org/3/library/collections.html#collections.namedtuple"><code>collections.namedtuple</code></a> and <a rel="nofollow" class="external text" href="https://docs.python.org/3/library/typing.html#typing.NamedTuple"><code>typing.NamedTuple</code></a>, available from Python 3.6 onward, create simple immutable classes. The following example is roughly equivalent to the above, plus some tuple-like features:
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="kn">from</span><span class="w"> </span><span class="nn">typing</span><span class="w"> </span><span class="kn">import</span> <span class="n">NamedTuple</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">collections</span>

<span class="n">Point</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="s2">"Point"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"x"</span><span class="p">,</span> <span class="s2">"y"</span><span class="p">])</span>

<span class="c1"># the following creates a similar namedtuple to the above</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Point</span><span class="p">(</span><span class="n">NamedTuple</span><span class="p">):</span>
<span class="n">x</span><span class="p">:</span> <span class="nb">int</span>
<span class="n">y</span><span class="p">:</span> <span class="nb">int</span>
</pre></div>
<p>Introduced in Python 3.7, <a rel="nofollow" class="external text" href="https://docs.python.org/3/library/dataclasses.html"><code>dataclasses</code></a> allow developers to emulate immutability with <a rel="nofollow" class="external text" href="https://docs.python.org/3/library/dataclasses.html#frozen-instances">frozen instances</a>. If a frozen dataclass is built, <code>dataclasses</code> will override <code>__setattr__()</code> and <code>__delattr__()</code> to raise <code>FrozenInstanceError</code> if invoked.
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="kn">from</span><span class="w"> </span><span class="nn">dataclasses</span><span class="w"> </span><span class="kn">import</span> <span class="n">dataclass</span>

<span class="nd">@dataclass</span><span class="p">(</span><span class="n">frozen</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Point</span><span class="p">:</span>
<span class="n">x</span><span class="p">:</span> <span class="nb">int</span>
<span class="n">y</span><span class="p">:</span> <span class="nb">int</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Racket">Racket</h3></div>
<p><a href="Racket_(programming_language)" title="Racket (programming language)">Racket</a> substantially diverges from other <a href="Scheme_(programming_language)" title="Scheme (programming language)">Scheme</a> implementations by making its core pair type ("cons cells") immutable. Instead, it provides a parallel mutable pair type, via <code>mcons</code>, <code>mcar</code>, <code>set-mcar!</code> etc. In addition, many immutable types are supported, for example, immutable strings and vectors, and these are used extensively. New structs are immutable by default, unless a field is specifically declared mutable, or the whole struct:
</p>
<div class="mw-highlight mw-highlight-lang-racket mw-content-ltr" dir="ltr"><pre><span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="n">foo1</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="n">y</span><span class="p">))</span><span class="w"> </span><span class="c1">; all fields immutable</span>
<span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="n">foo2</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="p">[</span><span class="n">y</span><span class="w"> </span><span class="kd">#:mutable</span><span class="p">]))</span><span class="w"> </span><span class="c1">; one mutable field</span>
<span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="n">foo3</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="kd">#:mutable</span><span class="p">)</span><span class="w"> </span><span class="c1">; all fields mutable</span>
</pre></div>
<p>The language also supports immutable hash tables, implemented functionally, and immutable dictionaries.
</p>
<div class="mw-heading mw-heading3"><h3 id="Rust">Rust</h3></div>
<p>Rust's <a href="Rust_(programming_language)#Ownership" title="Rust (programming language)">ownership</a> system allows developers to declare immutable variables, and pass immutable references. By default, all variables and references are immutable. Mutable variables and references are explicitly created with the <code>mut</code> keyword.
</p><p><a rel="nofollow" class="external text" href="https://doc.rust-lang.org/reference/items/constant-items.html">Constant items</a> in Rust are always immutable.
</p>
<div class="mw-highlight mw-highlight-lang-rust mw-content-ltr" dir="ltr"><pre><span class="c1">// constant items are always immutable</span>
<span class="k">const</span><span class="w"> </span><span class="n">ALWAYS_IMMUTABLE</span><span class="p">:</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>

<span class="k">struct</span><span class="w"> </span><span class="nc">Object</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span>
<span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span>
<span class="p">}</span>

<span class="k">fn</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// explicitly declare a mutable variable</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">mutable_obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Object</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="n">mutable_obj</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="c1">// okay</span>

<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">mutable_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">mutable_obj</span><span class="p">;</span>
<span class="w"> </span><span class="n">mutable_ref</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// okay</span>

<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">immutable_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mutable_obj</span><span class="p">;</span>
<span class="w"> </span><span class="n">immutable_ref</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="c1">// error E0594</span>

<span class="w"> </span><span class="c1">// by default, variables are immutable</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">immutable_obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Object</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="n">immutable_obj</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">6</span><span class="p">;</span><span class="w"> </span><span class="c1">// error E0596</span>

<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">mutable_ref2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>
<span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">immutable_obj</span><span class="p">;</span><span class="w"> </span><span class="c1">// error E0596</span>

<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">immutable_ref2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">immutable_obj</span><span class="p">;</span>
<span class="w"> </span><span class="n">immutable_ref2</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">6</span><span class="p">;</span><span class="w"> </span><span class="c1">// error E0594</span>
<span class="w"> </span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Scala">Scala</h3></div>
<p>In <a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a>, any entity (narrowly, a binding) can be defined as mutable or immutable: in the declaration, one can use <code>val</code> (value) for immutable entities and <code>var</code> (variable) for mutable ones. Note that even though an immutable binding can not be reassigned, it may still refer to a mutable object and it is still possible to call mutating methods on that object: the <i>binding</i> is immutable, but the underlying <i>object</i> may be mutable.
</p><p>For example, the following code snippet:
</p>
<div class="mw-highlight mw-highlight-lang-scala mw-content-ltr" dir="ltr"><pre><span class="kd">val</span><span class="w"> </span><span class="n">maxValue</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span>
<span class="kd">var</span><span class="w"> </span><span class="n">currentValue</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span>
</pre></div>
<p>defines an immutable entity <code>maxValue</code> (the integer type is inferred at compile-time) and a mutable entity named <code>currentValue</code>.
</p><p>By default, collection classes such as <code>List</code> and <code>Map</code> are immutable, so update-methods return a new instance rather than mutating an existing one. While this may sound inefficient, the implementation of these classes and their guarantees of immutability mean that the new instance can re-use existing nodes, which, especially in the case of creating copies, is very efficient.<sup id="cite_ref-17" class="reference"><a href="#cite_note-17"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Clojure" title="Clojure">Clojure</a></li>
<li><a href="Erlang_(programming_language)" title="Erlang (programming language)">Erlang</a></li>
<li><a href="F_Sharp_(programming_language)" title="F Sharp (programming language)">F#</a></li>
<li><a href="Haskell_(programming_language)" class="mw-redirect" title="Haskell (programming language)">Haskell</a></li>
<li><a href="Mutator_method" title="Mutator method">Mutator method</a></li>
<li><a href="Prolog" title="Prolog">Prolog</a></li>
<li><a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a></li>
<li><a href="Tcl" title="Tcl">Tcl</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<p><i>This article contains some material from the <a href="Perl_Design_Patterns_Book" title="Perl Design Patterns Book">Perl Design Patterns Book</a></i>
</p>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.oxfordlearnersdictionaries.com/definition/english/immutable">"immutable adjective - Definition, pictures, pronunciation and usage notes - Oxford Advanced Learner's Dictionary at OxfordLearnersDictionaries.com"</a>. <i>www.oxfordlearnersdictionaries.com</i>.</cite></span>
</li>
<li id="cite_note-Goetz-2"><span class="mw-cite-backlink">^ <a href="#cite_ref-Goetz_2-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-Goetz_2-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-Goetz_2-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text">Goetz et al. <i>Java Concurrency in Practice</i>. Addison Wesley Professional, 2006, Section 3.4. Immutability</span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.mit.edu/6.005/www/fa16/classes/09-immutability/">"6.005 — Software Construction"</a>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFDavid_O'Meara2003" class="citation web cs1">David O'Meara (April 2003). <a rel="nofollow" class="external text" href="http://www.javaranch.com/journal/2003/04/immutable.htm">"Mutable and Immutable Objects: Make sure methods can't be overridden"</a>. <i>Java Ranch</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2012-05-14</span></span>. <q>The preferred way is to make the class final. This is sometimes referred to as "Strong Immutability". It prevents anyone from extending your class and accidentally or deliberately making it mutable.</q></cite></span>
</li>
<li id="cite_note-Bloch-5"><span class="mw-cite-backlink">^ <a href="#cite_ref-Bloch_5-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-Bloch_5-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite id="CITEREFBloch2018" class="citation book cs1">Bloch, Joshua (2018). <i>"Effective Java: Programming Language Guide"</i> (third&nbsp;ed.). Addison-Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-0134685991</bdi>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.python.org/release/3.0/library/functions.html#bytearray">"Built-in Functions — Python v3.0 documentation"</a>. <i>docs.python.org</i>.</cite></span>
</li>
<li id="cite_note-Skeet-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-Skeet_7-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFSkeet2019" class="citation book cs1">Skeet, Jon (23 March 2019). <i>C# in Depth</i>. Manning. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-1617294532</bdi>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/records">"Use record types - C# tutorial - C#"</a>. <i>learn.microsoft.com</i>. 14 November 2023<span class="reference-accessdate">. Retrieved <span class="nowrap">23 February</span> 2024</span>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record">"Records - C# reference - C#"</a>. <i>learn.microsoft.com</i>. 25 May 2023<span class="reference-accessdate">. Retrieved <span class="nowrap">23 February</span> 2024</span>.</cite></span>
</li>
<li id="cite_note-d_spec_const-10"><span class="mw-cite-backlink">^ <a href="#cite_ref-d_spec_const_10-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-d_spec_const_10-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://dlang.org/spec/const3.html">D Language Specification §&nbsp;18</a></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://dlang.org/spec/arrays.html#strings">D Language Specification §&nbsp;12.16</a> (The terms <i>array</i> and <i>slice</i> are used interchangeably.)</span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://javarevisited.blogspot.co.uk/2013/03/how-to-create-immutable-class-object-java-example-tutorial.html">"How to create Immutable Class and Object in Java – Tutorial Example"</a>. Javarevisited.blogspot.co.uk. 2013-03-04<span class="reference-accessdate">. Retrieved <span class="nowrap">2014-04-14</span></span>.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.javapractices.com/topic/TopicAction.do?Id=29">"Immutable objects"</a>. javapractices.com<span class="reference-accessdate">. Retrieved <span class="nowrap">November 15,</span> 2012</span>.</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://desalasworks.com/article/immutability-in-javascript-a-contrarian-view/">"Immutability in JavaScript: A Contrarian View"</a>. <i>Desalasworks</i>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external free" href="https://www.php.net/releases/8.1/en.php#readonly_properties">https://www.php.net/releases/8.1/en.php#readonly_properties</a></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external free" href="https://www.php.net/releases/8.2/en.php#readonly_classes">https://www.php.net/releases/8.2/en.php#readonly_classes</a></span>
</li>
<li id="cite_note-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-17">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.scala-lang.org/docu/files/collections-api/collections_12.html">"Scala 2.8 Collections API – Concrete Immutable Collection Classes"</a>. Scala-lang.org<span class="reference-accessdate">. Retrieved <span class="nowrap">2014-04-14</span></span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1290876196">
/* start https://en.wikipedia.org/ */


.mw-parser-output .side-box{margin:4px 0;box-sizing:border-box;border:1px solid #aaa;font-size:88%;line-height:1.25em;background-color:var(--background-color-interactive-subtle,#f8f9fa);display:flow-root}.mw-parser-output .infobox .side-box{font-size:100%}.mw-parser-output .side-box-abovebelow,.mw-parser-output .side-box-text{padding:0.25em 0.9em}.mw-parser-output .side-box-image{padding:2px 0 2px 0.9em;text-align:center}.mw-parser-output .side-box-imageright{padding:2px 0.9em 2px 0;text-align:center}@media(min-width:500px){.mw-parser-output .side-box-flex{display:flex;align-items:center}.mw-parser-output .side-box-text{flex:1;min-width:0}}@media(min-width:720px){.mw-parser-output .side-box{width:238px}.mw-parser-output .side-box-right{clear:right;float:right;margin-left:1em}.mw-parser-output .side-box-left{margin-right:1em}}


/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1237033735">
/* start https://en.wikipedia.org/ */


@media print{body.ns-0 .mw-parser-output .sistersitebox{display:none!important}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}


/* end https://en.wikipedia.org/ */
</style><div class="side-box side-box-right sistersitebox"><style data-mw-deduplicate="TemplateStyles:r1126788409">
/* start https://en.wikipedia.org/ */


.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}


/* end https://en.wikipedia.org/ */
</style>
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">Look up <i><b><a href="https://en.wiktionary.org/wiki/mutable" class="extiw external" title="wiktionary:mutable">mutable</a></b></i> in Wiktionary, the free dictionary.</div></div>
</div>
<ul><li><a rel="nofollow" class="external text" href="https://www.codeproject.com/Articles/1043301/Immutable-objects-in-Csharp">Immutable objects in C#</a> using 3 simple steps.</li>
<li>Article <a rel="nofollow" class="external text" href="https://web.archive.org/web/20101020235317/http://www.ibm.com/developerworks/java/library/j-jtp02183.html">Java theory and practice: To mutate or not to mutate?</a> by Brian Goetz, from IBM DeveloperWorks – <a rel="nofollow" class="external text" href="https://web.archive.org/web/20101020235317/http://www.ibm.com/developerworks/java/library/j-jtp02183.html">saved copy at Internet Archive</a> by Brian Goetz, from IBM DeveloperWorks – <a rel="nofollow" class="external text" href="https://web.archive.org/web/20101020235317/http://www.ibm.com/developerworks/java/library/j-jtp02183.html">saved copy at Internet Archive</a></li>
<li><a rel="nofollow" class="external text" href="http://www.javapractices.com/Topic29.cjp">Immutable objects</a> from JavaPractices.com</li>
<li><a rel="nofollow" class="external text" href="http://c2.com/cgi/wiki?ImmutableObject">Immutable objects</a> from <a href="Portland_Pattern_Repository" title="Portland Pattern Repository">Portland Pattern Repository</a></li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20150809185757/http://facebook.github.io/immutable-js/">Immutable.js</a> by Facebook</li>
<li><a rel="nofollow" class="external text" href="https://persistent.codeplex.com/">Immutable structures in C#</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20171221010951/http://persistent.codeplex.com/">Archived</a> 2017-12-21 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a> opensource project in Codeplex</li>
<li><a rel="nofollow" class="external text" href="https://msdn.microsoft.com/en-us/library/system.collections.immutable(v=vs.111).aspx">Immutable collections in .NET</a> official library by Microsoft</li>
<li><a rel="nofollow" class="external text" href="https://www.tutlane.com/tutorial/csharp/csharp-string-with-examples#divcsstimu">Immutable objects in C#</a> by Tutlane.com</li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-08-02" href="https://en.wikipedia.org/wiki/?title=Immutable_object&amp;oldid=1303900666">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>